package com.liruan.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.liruan.rest.controller.response.JSONResponse;
import com.liruan.rest.dto.AuthTokenDTO;
import com.liruan.rest.dto.LogginDTO;
import com.liruan.rest.dto.UserRegisterDTO;
import com.liruan.rest.service.UserService;
import com.liruan.rest.service.result.ResultCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags = "用户管理")
@RestController
@RequestMapping("user")
public class UserController
{
	@Autowired
	private UserService userService;

	@ApiResponses(value = {@ApiResponse(code = ResultCode.GENERAL_SUCCESS_CODE, message = ResultCode.GENERAL_SUCCESS_TIP), @ApiResponse(code = ResultCode.INVALID_PARAM_CODE, message = ResultCode.INVALID_PARAM_TIP), @ApiResponse(code = ResultCode.DB_ACESSS_FAIL_CODE, message = ResultCode.DB_ACESSS_FAIL_TIP)})
	@ApiOperation(value = "用户登录", httpMethod = "POST", consumes = "application/json")
	@RequestMapping(path = "loggin", method = RequestMethod.POST, consumes = "application/json")
	public JSONResponse<AuthTokenDTO> loggin(@RequestBody LogginDTO dto)
	{
		AuthTokenDTO result = userService.loggin(dto);
		return new JSONResponse<AuthTokenDTO>(result.getResultCode(), result);
	}
	
	@ApiResponses(value = {@ApiResponse(code = ResultCode.GENERAL_SUCCESS_CODE, message = ResultCode.GENERAL_SUCCESS_TIP), @ApiResponse(code = ResultCode.INVALID_PARAM_CODE, message = ResultCode.INVALID_PARAM_TIP), @ApiResponse(code = ResultCode.DB_ACESSS_FAIL_CODE, message = ResultCode.DB_ACESSS_FAIL_TIP)})
	@ApiOperation(value = "用户注册", httpMethod = "POST", consumes = "application/json")
	@RequestMapping(path = "", method = RequestMethod.POST, consumes = "application/json")
	public JSONResponse<AuthTokenDTO> register(@RequestBody UserRegisterDTO dto)
	{
		AuthTokenDTO res = userService.register(dto);
		return new JSONResponse<AuthTokenDTO>(res.getResultCode(), res);
	}
}
